package algorithm

// BubbleSort 冒泡排序
func BubbleSort(arr []int) {
	length := len(arr)
	for i := 0; i < length; i++ {
		for j := 0; j < length-i-1; j++ {
			if arr[j] > arr[j+1] {
				arr[j], arr[j+1] = arr[j+1], arr[j]
			}
		}
	}
}

func InsertionSorting(arr []int) {
	length := len(arr)
	for i := 1; i < length; i++ {
		for j := i - 1; j >= 0; j-- {
			if arr[j] > arr[j+1] {
				arr[j], arr[j+1] = arr[j+1], arr[j]
			} else {
				break
			}
		}
	}
}

func SelectionSort(arr []int) {
	length := len(arr)
	var minValue int
	var minIndex int
	for i := 0; i < length; i++ {
		minValue = arr[i]
		for j := i + 1; j < length; j++ {
			if arr[j] < minValue {
				minValue = arr[j]
				minIndex = j
			}
		}
		if minValue != arr[i] {
			arr[i], arr[minIndex] = arr[minIndex], arr[i]
		}
	}
}
